﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml.Serialization;

namespace Dynamic.Testing.Learn
{
    public class SerializationTest
    {

        public static void Go()
        {
            MyObject obj = new MyObject();
            obj.Birthday = new DateTime(1979, 11, 7);
            obj.HomePlace = "湖北";
            obj.Name = "周公";
            Console.WriteLine("======== 使用BinaryFormatter类进行序列化和反序列化。====");
            BinarySerialize(obj);
            BinaryDeserialize("C:\\MyObject.dat");
            Console.WriteLine("======== 使用SoapFormatter类进行序列化和反序列化。====");
            SOAPSerialize(obj);
            SOAPDeserialize("C:\\MyObject.soap");
            Console.WriteLine("======== 使用XmlSerializer类进行序列化和反序列化。====");
            XMLSerialize(obj);
            XMLDeserialize("C:\\MyObject.xml");
        }

        /// <summary>  
        /// 二进制序列化对象  
        /// </summary>  
        /// <param name="obj"></param>  
        public static void BinarySerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.dat", FileMode.Create, FileAccess.Write))
            {
                BinaryFormatter formater = new BinaryFormatter();
                formater.Serialize(stream, obj);
                Console.WriteLine(" 对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>  
        /// 二进制反序列化  
        /// </summary>  
        /// <param name="fileName"></param>  
        public static void BinaryDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                BinaryFormatter formater = new BinaryFormatter();
                MyObject obj = (MyObject)formater.Deserialize(stream);
                Console.WriteLine("对象已经被反序列化。" + obj.ToString());
            }
        }

        /// <summary>  
        /// 二进制序列化对象  
        /// </summary>  
        /// <param name="obj"></param>  
        public static void SOAPSerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.soap", FileMode.Create, FileAccess.Write))
            {
                SoapFormatter formater = new SoapFormatter();
                formater.Serialize(stream, obj);
                Console.WriteLine("对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>  
        /// 二进制反序列化  
        /// </summary>  
        /// <param name="fileName"></param>  
        public static void SOAPDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                SoapFormatter formater = new SoapFormatter();
                MyObject obj = (MyObject)formater.Deserialize(stream);
                Console.WriteLine(" 对象已经被反序列化。" + obj.ToString());
            }
        }
        /// <summary>  
        /// XML序列化  
        /// </summary>  
        /// <param name="obj"></param>  
        public static void XMLSerialize(MyObject obj)
        {
            using (FileStream stream = new FileStream("C:\\MyObject.xml", FileMode.Create, FileAccess.Write))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                ns.Add("", string.Empty);
                serializer.Serialize(stream, obj,ns);
                
                Console.WriteLine("对象已经被序列化。" + obj.ToString());
            }
        }
        /// <summary>  
        /// XML反序列化  
        /// </summary>  
        /// <param name="fileName"></param>  
        public static void XMLDeserialize(string fileName)
        {
            using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(MyObject));
                MyObject obj = (MyObject)serializer.Deserialize(stream);
                Console.WriteLine(" 对象已经被反序列化。" + obj.ToString());
            }
        }

    }



    /// <summary>  
    /// 要序列化的对象  
    /// 作者：周公  
    /// 编写时间：2009-03-10  
    /// </summary>  
    [Serializable]
    public class MyObject
    {
        //[NonSerialized]  
        private string name;
        private DateTime birthday;
        private string homePlace;

        /// <summary>  
        /// 出生地  
        /// </summary>  
        public string HomePlace
        {
            get { return homePlace; }
            set { homePlace = value; }
        }

        /// <summary>  
        /// 生日  
        /// </summary>  
        public DateTime Birthday
        {
            get { return birthday; }
            set { birthday = value; }
        }

        /// <summary>  
        /// 姓名  
        /// </summary>  
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>  
        /// 年龄  
        /// </summary>  
        public int Age
        {
            get { return DateTime.Now.Year - birthday.Year; }
        }
        /// <summary>  
        /// override了ToString()方法  
        /// </summary>  
        /// <returns></returns>  
        public override string ToString()
        {
            return string.Format("姓名：{0},生日：{1},出生地：{2},年龄：{3}", name, birthday, homePlace, Age);
        }
    }
}
